/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.jctc.els.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jctc.els.modules.biz.entity.projects.ElsProjects;
import org.jctc.els.modules.biz.excel.ElsProjectsExcel;
import org.jctc.els.modules.biz.excel.ElssystemlogExcel;
import org.jctc.els.modules.biz.mapper.LogApiMapper;
import org.jctc.els.modules.biz.service.ILogApiService;
import org.jctc.els.modules.biz.vo.LogApiVO;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.log.model.LogApi;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Objects;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
public class LogApiServiceImpl extends ServiceImpl<LogApiMapper, LogApi> implements ILogApiService {

	@Autowired
	private LogApiMapper logApiMapper;

	@Override
	public IPage<LogApiVO> selectLogApiPage(IPage<LogApiVO> page, LogApiVO logApi) {
		return page.setRecords(baseMapper.selectLogApiPage(page, logApi));
	}

	@Override
	public LogApi selectById(Long id) {
		return null;
	}

	@Override
	public IPage<LogApi> selectAllPage(Integer pageNum, Integer pageSize, String createdBy, String logType, String createTime,Long customerId) {
		Page<LogApi> logApiPage = new Page<>(pageNum, pageSize);
		Page<LogApi> logApiPage1 = logApiMapper.selectAllPage(logApiPage,createdBy,logType,createTime,customerId);
		return logApiPage1;
	}

	@Override
	public IPage<LogApi> selectAllPageOther(Integer pageNum, Integer pageSize, String createdBy, String logType, String createTime) {
		Page<LogApi> logApiPage = new Page<>(pageNum, pageSize);
		Page<LogApi> logApiPage1 = logApiMapper.selectAllPageOther(logApiPage,createdBy,logType,createTime);
		return logApiPage1;
	}

	@Override
	public List<ElssystemlogExcel> ExportPage(Wrapper<LogApi> queryWrapper) {
		  List<ElssystemlogExcel> excelIPage=  logApiMapper.exportPages(queryWrapper);
		  return excelIPage;
	}

	@Override
	public void importSystemlog(MultipartFile multipartFile, HttpServletRequest request) {
//		当前文件获取路径
		String filePath = request.getSession().getServletContext().getRealPath("/");
		//根据地址和文件名准确获取用户上传的表格
		File tempFile = new File(filePath + multipartFile.getOriginalFilename());
		List<ElssystemlogExcel> data = ExcelUtil.read(multipartFile, ElssystemlogExcel.class);
//        BladeUser loginUser = AuthUtil.getUser();
		data.forEach(ElssystemlogExcel->{
			LogApi logApi = Objects.requireNonNull(BeanUtil.copy(ElssystemlogExcel, LogApi.class));
//            elsProjects.setUpdatedBy(loginUser.getUserId());
			logApi.setCreatedTime(DateUtil.now());
//            elsProjects.setCreatedBy(loginUser.getUserId());
			this.save(logApi);
		});
	}
}
